//
//  LCAlertTableView.m
//  LCInputView
//
//  Created by Rochang on 2017/4/18.
//  Copyright © 2017年 Rochang. All rights reserved.
//

#import "LCAlertTableView.h"
#import "BluetoothCenter.h"
#import "LCPeripherCell.h"

@interface LCAlertTableView ()<UITableViewDataSource, UITableViewDelegate, BluetoothCenterDelegate>

@property (weak, nonatomic) IBOutlet UIView *maskView;
@property (weak, nonatomic) IBOutlet UIView *tipView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray<CBPeripheral *> *peripherals;
@property (nonatomic, strong) NSIndexPath *selectIndexPath;

@end

@implementation LCAlertTableView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.titleLabel.textColor = LCTextColor;
    self.tipView.layer.cornerRadius = 5;
    self.tipView.clipsToBounds = YES;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LCPeripherCell class]) bundle:nil] forCellReuseIdentifier:[LCPeripherCell identy]];
    [self.peripherals removeAllObjects];
}

+ (instancetype)getAlertTableView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
}

- (void)showWihtTitle:(NSString *)title{
    self.titleLabel.text = title;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.tipView.alpha = 0;
    self.alpha = 0;
    self.tipView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tipView.alpha = 1.0;
        weakSelf.alpha = 1.0;
        weakSelf.tipView.transform = CGAffineTransformIdentity;
    }];
    // 开始扫描
    [BluetoothCenter shareInstance].delegate = self;
    [[BluetoothCenter shareInstance] scanperipheral];
}

- (IBAction)close:(id)sender {
    [self hideWithAnimation:YES];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.peripherals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCPeripherCell *cell = [tableView dequeueReusableCellWithIdentifier:[LCPeripherCell identy]];
    CBPeripheral *peripheral = self.peripherals[indexPath.row];
    cell.titleLabel.text = peripheral.name;
    if ([[BluetoothCenter shareInstance].currentPeripheral isEqual:peripheral]) {
        cell.selectImageView.hidden = NO;
    } else {
        cell.selectImageView.hidden = YES;
    }
    return cell;
}

#pragma mark - BluetoothCenterDelegate
- (void)bluetoothCenterDidScandPeripheral:(CBPeripheral *)peripheral {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.peripherals.count inSection:0];
    [self.peripherals addObject:peripheral];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LCPeripherCell *selectcell = [tableView cellForRowAtIndexPath:self.selectIndexPath];
    selectcell.selectImageView.hidden = YES;
    LCPeripherCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectImageView.hidden = NO;
    self.selectIndexPath = indexPath;
    // 连接设备
    CBPeripheral *peripheral = self.peripherals[indexPath.row];
    [[BluetoothCenter shareInstance] connetPeripheral:peripheral];
    
    [self hideWithAnimation:YES];
}

- (void)hideWithAnimation:(BOOL)animtaion {
    [[BluetoothCenter shareInstance] stopScand];
    if (!animtaion) {
        [self removeFromSuperview];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.tipView.alpha = 0;
        weakSelf.alpha = 0;
        weakSelf.tipView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - getter
- (NSMutableArray<CBPeripheral *> *)peripherals {
    if (!_peripherals) {
        _peripherals = [[NSMutableArray alloc] init];
    }
    return _peripherals;
}

@end
